<template>
  <div>
    <div  v-if="opendetail == false" class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">

<!--      <el-form-item label="商户编号" prop="merchantCode">-->
<!--        <el-input-->
<!--            v-model="queryParams.merchantCode"-->
<!--            placeholder="请输入商户编号"-->
<!--            clearable-->
<!--            @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="负责人姓名" prop="contactName" label-width="100px">
        <el-input
            v-model="queryParams.contactName"
            placeholder="请输入负责人姓名"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>

<!--      <el-form-item label="营业执照编号" prop="businessLicenseNumber" label-width="100px">-->
<!--        <el-input-->
<!--            v-model="queryParams.businessLicenseNumber"-->
<!--            placeholder="请输入营业执照编号"-->
<!--            clearable-->
<!--            @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="店名" prop="shopName">
        <el-input
            v-model="queryParams.shopName"
            placeholder="请输入店名"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item label="经营区域" prop="businessArea">
        <el-input
            v-model="queryParams.businessArea"
            placeholder="请输入经营区域"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="详细地址" prop="detailedAddress">
        <el-input
            v-model="queryParams.detailedAddress"
            placeholder="请输入详细地址"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item label="审核状态" prop="auditStatus">
        <el-select v-model="queryParams.auditStatus" placeholder="请选择审核状态" clearable style="width: 150px">
          <el-option
              v-for="dict in sys_audit_state"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>



    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['masterrabbit-kaixuan:merchants:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="merchantsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="ID" align="center" prop="merchantId" />-->
<!--      <el-table-column label="商户编号" align="center" prop="merchantCode" />-->
      <el-table-column label="负责人姓名" align="center" prop="contactName" />
      <el-table-column label="营业执照编号" align="center" prop="businessLicenseNumber" />
      <el-table-column label="店名" align="center" prop="shopName" />
      <el-table-column label="经营区域" align="center" prop="businessArea" />
      <el-table-column label="详细地址" align="center" prop="detailedAddress" />
      <el-table-column label="审核状态" align="center" prop="auditStatus">
        <template #default="scope">
          <dict-tag :options="sys_audit_state" :value="scope.row.auditStatus" :class="{ cross: scope.row.auditStatus == 2 ,uncross: scope.row.auditStatus == 3 }" />
        </template>
      </el-table-column>
      <el-table-column label="营业时间" align="center" prop="businessHours" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
<!--          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['masterrabbit-kaixuan:merchants:edit']">修改</el-button>-->
<!--          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['masterrabbit-kaixuan:merchants:remove']">删除</el-button>-->
          <el-button link type="primary"  @click="getdetail(scope.row)" >详情</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改商户信息对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="merchantsRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="商户编号" prop="merchantCode">
          <el-input v-model="form.merchantCode" placeholder="请输入商户编号" />
        </el-form-item>
        <el-form-item label="负责人姓名" prop="contactName">
          <el-input v-model="form.contactName" placeholder="请输入负责人姓名" />
        </el-form-item>
        <el-form-item label="身份证号" prop="idCardNumber">
          <el-input v-model="form.idCardNumber" placeholder="请输入身份证号" />
        </el-form-item>
        <el-form-item label="营业执照编号" prop="businessLicenseNumber">
          <el-input v-model="form.businessLicenseNumber" placeholder="请输入营业执照编号" />
        </el-form-item>
        <el-form-item label="店名" prop="shopName">
          <el-input v-model="form.shopName" placeholder="请输入店名" />
        </el-form-item>
        <el-form-item label="手机号" prop="phoneNumber">
          <el-input v-model="form.phoneNumber" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="经营区域" prop="businessArea">
          <el-input v-model="form.businessArea" placeholder="请输入经营区域" />
        </el-form-item>
        <el-form-item label="详细地址" prop="detailedAddress">
          <el-input v-model="form.detailedAddress" placeholder="请输入详细地址" />
        </el-form-item>
        <el-form-item label="身份证正反照id" prop="idCardImageId">
          <el-input v-model="form.idCardImageId" placeholder="请输入身份证正反照id" />
        </el-form-item>
        <el-form-item label="营业执照照片id" prop="businessLicenseImageId">
          <el-input v-model="form.businessLicenseImageId" placeholder="请输入营业执照照片id" />
        </el-form-item>
        <el-form-item label="门面照片id" prop="storefrontImageId">
          <el-input v-model="form.storefrontImageId" placeholder="请输入门面照片id" />
        </el-form-item>
        <el-form-item label="经度" prop="longitude">
          <el-input v-model="form.longitude" placeholder="请输入经度" />
        </el-form-item>
        <el-form-item label="维度" prop="latitude">
          <el-input v-model="form.latitude" placeholder="请输入维度" />
        </el-form-item>
        <el-form-item label="审核状态" prop="auditStatus">
          <el-radio-group v-model="form.auditStatus">
            <el-radio
                v-for="dict in sys_audit_state"
                :key="dict.value"
                :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="营业时间" prop="businessHours">
          <el-input v-model="form.businessHours" placeholder="请输入营业时间" />
        </el-form-item>
        <el-form-item label="userid" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入userid" />
        </el-form-item>
        <el-form-item label="店铺余额" prop="shopBalance">
          <el-input v-model="form.shopBalance" placeholder="请输入店铺余额" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
    <div v-if="opendetail == true">
      <el-form>
        <el-button type="primary" @click="backlast">返回</el-button>
      </el-form>
      <el-card class="box-card">
        <el-form label-width="120px" label-position="left">
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="商店名">
                <span>{{ details.shopName }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="商店ID">
                <span>{{ details.merchantId }}</span>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="负责人姓名">
                <span>{{ details.contactName }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="负责人电话">
                <span>{{ details.phoneNumber }}</span>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="身份证">
                <span>{{ details.idCardNumber }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="经营区域">
                <span>{{ details.businessArea }}</span>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="详细地址">
                <span>{{ details.detailedAddress }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="营业时间">
                <span>{{ details.businessHours }}</span>
              </el-form-item>
            </el-col>
          </el-row>

          <el-form-item label="老板用户名">
            <span>{{ details.userId }}</span>
          </el-form-item>
          <el-form-item label="审核状态">

              <dict-tag :options="sys_audit_state" :value="details.auditStatus" :class="{ cross: details.auditStatus == 2 ,uncross: details.auditStatus == 3 }" />

          </el-form-item>

          <div>商户图片</div>
          <el-form-item>
            <el-row :gutter="20"  style="display: flex;justify-content: space-around;">
              <el-form-item  label="营业执照">
                <el-col :span="8">
                  <el-image v-if="details.businessLicenseImageId" style="width: 100px; height: 100px" src="" fit="cover" />
                </el-col>
              </el-form-item>
              <el-form-item  label="商户门面照">
                <el-col :span="8">
                  <el-image v-if="details.storefrontImageId" style="width: 100px; height: 100px" src="" fit="cover" />
                </el-col>
              </el-form-item>
              <el-form-item  label="负责人身份证">
                <el-col :span="8">
                  <el-image v-if="details.idCardImageId" style="width: 100px; height: 100px" src="" fit="cover" />
                </el-col>
                </el-form-item>
            </el-row>
          </el-form-item>
        </el-form>
        <el-form>
          <el-button type="primary"  style="color: black" :disabled="details.auditStatus !== 1" @click="crosshandleUpdate(details)" >审核通过</el-button>
          <el-button type="primary" style="background-color: #ed5565" :disabled="details.auditStatus !== 1"  @click="uncrosshandleUpdate(details)" >审核不通过</el-button>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<script setup name="Merchants">
import { listMerchants, getMerchants, delMerchants, addMerchants, updateMerchants } from "@/api/masterrabbit-kaixuan/merchants";

const { proxy } = getCurrentInstance();
const { sys_audit_state } = proxy.useDict('sys_audit_state');


const merchantsList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    merchantId: null,
    merchantCode: null,
    contactName: null,
    idCardNumber: null,
    businessLicenseNumber: null,
    shopName: null,
    phoneNumber: null,
    businessArea: null,
    detailedAddress: null,
    idCardImageId: null,
    businessLicenseImageId: null,
    storefrontImageId: null,
    longitude: null,
    latitude: null,
    auditStatus: null,
    businessHours: null,
    userId: null,
    shopBalance: null
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询商户信息列表 */
function getList() {
  loading.value = true;
  listMerchants(queryParams.value).then(response => {
    merchantsList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    merchantId: null,
    merchantCode: null,
    contactName: null,
    idCardNumber: null,
    businessLicenseNumber: null,
    shopName: null,
    phoneNumber: null,
    businessArea: null,
    detailedAddress: null,
    idCardImageId: null,
    businessLicenseImageId: null,
    storefrontImageId: null,
    longitude: null,
    latitude: null,
    auditStatus: null,
    businessHours: null,
    userId: null,
    shopBalance: null
  };
  proxy.resetForm("merchantsRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.merchantId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加商户信息";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _merchantId = row.merchantId || ids.value
  getMerchants(_merchantId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改商户信息";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["merchantsRef"].validate(valid => {
    if (valid) {
      if (form.value.merchantId != null) {
        updateMerchants(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addMerchants(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _merchantIds = row.merchantId || ids.value;
  proxy.$modal.confirm('是否确认删除商户信息编号为"' + _merchantIds + '"的数据项？').then(function() {
    return delMerchants(_merchantIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('masterrabbit-kaixuan/merchants/export', {
    ...queryParams.value
  }, `merchants_${new Date().getTime()}.xlsx`)
}

const opendetail = ref(false)
const details = ref({})
/** 进入详情页 */
function getdetail(row){
  opendetail.value = true
  details.value = row
  console.log(row)

}
/** 退出详情页 */
function backlast(){
  opendetail.value = false

}

/** 通过审核 */
function crosshandleUpdate(row) {

  const _merchantId = row.merchantId || ids.value
  console.log(_merchantId)
  getMerchants(_merchantId).then(response => {
    reset();
    console.log(form.value)
    form.value = response.data;
    console.log(form.value)
    form.value.auditStatus = 2;
    title.value = "修改商户信息";
    updateMerchants(form.value).then(response => {
      proxy.$modal.msgSuccess("修改成功");
      open.value = false;
      getList();
    });
  });

  backlast()
}

/** 未通过审核 */
function uncrosshandleUpdate(row) {
  const _merchantId = row.merchantId || ids.value
  getMerchants(_merchantId).then(response => {
    reset();
    form.value = response.data;
    form.value.auditStatus = 3;
    title.value = "修改商户信息";
    updateMerchants(form.value).then(response => {
      proxy.$modal.msgSuccess("修改成功");
      open.value = false;
      getList();
    });
  });

  backlast()
}

getList();
</script>
<style scoped>

.uncross{
  color: red;
}

.cross{
  color: #25a825;
}

</style>
